package org.opens.sql.genHandle.impl;

import cn.hutool.core.util.StrUtil;
import org.opens.exception.BusinessException;
import org.opens.sql.constant.DeleteMode;
import org.opens.sql.constant.TombstoneColumnType;
import org.opens.sql.genConfig.GenSqlConfig;
import org.opens.sql.genConfig.TombstoneConfig;
import org.opens.sql.genHandle.GenHandle;

public class GenConstantHandle implements GenHandle {

    @Override
    public void genCode(GenSqlConfig genSqlConfig) {

    }

    @Override
    public void genCodeValid(GenSqlConfig genSqlConfig) {
        // 检测常量包是否配置, 开启逻辑删除的情况下需要检测
        if (genSqlConfig.getGenMapperPublicConfig().getDeleteMode() == DeleteMode.TOMBSTONE) {
            if (StrUtil.isBlank(genSqlConfig.getConstantPackageInfo())) {
                throw new IllegalArgumentException("未配置常量存储包!");
            }

            TombstoneConfig tombstoneConfig = genSqlConfig.getGenMapperPublicConfig().getTombstoneConfig();
            if (tombstoneConfig.getTombstoneColumnType() == TombstoneColumnType.CHAR) {
                if (StrUtil.isBlank(tombstoneConfig.getDeletedValue())) {
                    throw new BusinessException("逻辑删除的已删除值不能为空!");
                }
                if (StrUtil.isBlank(tombstoneConfig.getNotDeletedValue())) {
                    throw new BusinessException("逻辑删除的未删除值不能为空!");
                }
                if (tombstoneConfig.getDeletedValue().length() != 1) {
                    throw new IllegalArgumentException("逻辑删除的已删除值的长度只能为1");
                }
                if (tombstoneConfig.getNotDeletedValue().length() != 1) {
                    throw new IllegalArgumentException("逻辑删除的未删除值的长度只能为1");
                }
            }
        }
    }
}
